#!/usr/bin/env python
# coding:utf-8

import cyclone.auth
import cyclone.escape
import cyclone.web
from toughadmin.tools import utils
from toughadmin.console.admin.base import BaseHandler, MenuRes
from toughadmin.tools.permit import permit
from toughadmin.console import models
from toughadmin.console.admin import gwid_form

class GwidHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        self.render("gwid_list.html",
                    page_data=self.get_page_data(self.db.query(models.TraGwid)))

class AddHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        domains = [(m.domain_code, m.domain_desc) for m in self.db.query(models.TraDomain)]
        self.render("base_form.html", form=gwid_form.gwid_add_form(domains))

    @cyclone.web.authenticated
    def post(self):
        form = gwid_form.gwid_add_form([])
        if not form.validates(source=self.get_params()):
            return self.render("base_form.html", form=form)
        if self.db.query(models.TraGwid.id).filter_by(gwid=form.d.gwid).count() > 0:
            return self.render("base_form.html", form=form, msg=u"gwid已经存在")
        mgwid = models.TraGwid()
        mgwid.domain_code = form.d.domain_code
        mgwid.gwid = form.d.gwid
        mgwid.gwid_desc = form.d.gwid_desc
        self.db.add(mgwid)

        ops_log = models.TraOperateLog()
        ops_log.operator_name = self.get_secure_cookie("tra_user")
        ops_log.operate_ip = self.get_secure_cookie("tra_login_ip")
        ops_log.operate_time = utils.get_currtime()
        ops_log.operate_desc = u'操作员(%s)新增gwid信息:%s' % (ops_log.operator_name, mgwid.gwid)
        self.db.add(ops_log)

        self.db.commit()
        self.redirect("/gwid",permanent=False)


class UpdateHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        domains = [(m.domain_code, m.domain_desc) for m in self.db.query(models.TraDomain)]
        gwid_id = self.get_argument("gwid_id")
        mgwid = self.db.query(models.TraGwid).get(gwid_id)
        form = gwid_form.gwid_update_form(domains)
        form.fill(mgwid)
        return self.render("base_form.html", form=form)

    def post(self):
        form = gwid_form.gwid_update_form([])
        if not form.validates(source=self.get_params()):
            return self.render("base_form.html", form=form)
        mgwid = self.db.query(models.TraGwid).get(form.d.id)
        mgwid.domain_code = form.d.domain_code
        mgwid.gwid_desc = form.d.gwid_desc

        ops_log = models.TraOperateLog()
        ops_log.operator_name = self.get_secure_cookie("tra_user")
        ops_log.operate_ip = self.get_secure_cookie("tra_login_ip")
        ops_log.operate_time = utils.get_currtime()
        ops_log.operate_desc = u'操作员(%s)修改gwid信息:%s' % (ops_log.operator_name, mgwid.gwid)
        self.db.add(ops_log)

        self.db.commit()
        self.redirect("/gwid",permanent=False)


class DeleteHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        gwid_id = self.get_argument("gwid_id")
        self.db.query(models.TraGwid).filter_by(id=gwid_id).delete()

        ops_log = models.TraOperateLog()
        ops_log.operator_name = self.get_secure_cookie("tra_user")
        ops_log.operate_ip = self.get_secure_cookie("tra_login_ip")
        ops_log.operate_time = utils.get_currtime()
        ops_log.operate_desc = u'操作员(%s)删除gwid信息:%s' % (ops_log.operator_name, gwid_id)
        self.db.add(ops_log)

        self.db.commit()
        self.redirect("/gwid",permanent=False)


permit.add_route(GwidHandler, r"/gwid", u"GWID信息管理", MenuRes, order=2.0700, is_menu=True)
permit.add_route(AddHandler, r"/gwid/add", u"GWID信息新增", MenuRes, order=2.0701, is_menu=False)
permit.add_route(UpdateHandler, r"/gwid/update", u"GWID信息修改", MenuRes, order=2.0702, is_menu=False)
permit.add_route(DeleteHandler, r"/gwid/delete", u"GWID信息删除", MenuRes, order=2.0703, is_menu=False)